var JWT = require('../public/javascripts/jwt');
var express = require('express');
var router = express.Router();
const db = require('../db/index');

const getTokenInfo = (token) => {
  const tokenInfo = JWT.verifyToken(token.split(' ')[1]);
  return tokenInfo;
}

/* 获取card列表 */
router.get('/list', function(req, res, next) {
  const userInfo = getTokenInfo(req.get('Authorization'));
  const sql = `select id,name,img,textareaList from card where userId="${userInfo.id}"`;
  db.query(sql, (err, results) => {
    if (err) {
      res.send({
        code: 500,
        msg: err
      })
    } else {
      try {
        const list = results.map(item => {
          item.img && (item.img = item.img.split(','));
          item.textareaList = JSON.parse(item.textareaList);
          return item;
        })
        res.send({
          code: 200,
          data: {
            list
          },
          msg: '查询成功'
        });
      } catch (err) {
        next(err)
      }
    }
  })
});

/* 获取card详情 */
router.get('/', function(req, res, next) {
  const userInfo = getTokenInfo(req.get('Authorization'));
  const cardId = req.query.cardId;
  const sql = `select id,name,img,textareaList from card where userId="${userInfo.id}" and id = ${cardId}`;
  db.query(sql, (err, results) => {
    if (err) {
      res.send({
        code: 500,
        msg: err
      })
    } else {
      try {
        results[0].img && (results[0].img = results[0].img.split(','));
        results[0].textareaList = JSON.parse(results[0].textareaList);
        res.send({
          code: 200,
          data: results[0],
          msg: '查询成功'
        });
      } catch (err) {
        next(err);
      }
    }
  })
});

/* 新增card */
router.post('/', function(req, res, next) {
  const userInfo = getTokenInfo(req.get('Authorization'));
  const addInfo = req.body;
  addInfo.userId = userInfo.id;
  const sql = `insert into card set ?`;
  db.query(sql, addInfo, (err, results) => {
    if (err) {
      res.send({
        code: 500,
        msg: err
      })
    } else {
      try {
        res.send({
          code: 200,
          data: {
            id: results.insertId
          },
          msg: '新增成功'
        });
      } catch (err) {
        next(err);
      }
    }
  })
});

/* 修改card */
router.put('/', function(req, res, next) {
  const userInfo = getTokenInfo(req.get('Authorization'));
  const updateInfo = req.body;
  const sql = 'update card set ? where userId = ? and id = ?';
  db.query(sql, [updateInfo, userInfo.id, updateInfo.id], (err, results) => {
    if (err) {
      res.send({
        code: 500,
        msg: err
      })
    } else {
      try {
        res.send({
          code: 200,
          msg: '修改成功'
        });
      } catch (err) {
        next(err);
      }
    }
  })
});

/* 删除card */
router.delete('/', function(req, res, next) {
  const userInfo = getTokenInfo(req.get('Authorization'));
  const cardId = req.query.cardId;
  const sql = `delete from card where id = ${cardId}`;
  db.query(sql, (err, results) => {
    if (err) {
      res.send({
        code: 500,
        msdg: err
      })
    } else {
      try {
        res.send({
          code: 200,
          msg: '删除成功'
        });
      } catch (err) {
        next(err);
      }
    }
  })
});

module.exports = router;
